package com.cqeec.waimai4.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqeec.waimai4.entity.Employee;
import com.cqeec.waimai4.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// @Controller
// @ResponseBody

// @Controller+@ResponseBody=@RestController
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    /**
     * 查询employee表的全部数据
     */
    @PostMapping("/getList")
    public List<Employee> getList(){
        return employeeService.list();
    }

    /**
     * 根据id查询
     */
    @GetMapping("/getById/{id}")
    public Employee getById(@PathVariable("id") Integer id){
        Employee employee = employeeService.getById(id);
        return employee;
    }
    // 条件查询,查询电话中有237的所有数据
    @GetMapping("/getByPhone")
    public List<Employee> getByPhone(){
        //1、实例化条件构造器
        LambdaQueryWrapper<Employee> wrapper  = new LambdaQueryWrapper<>();
        //2、添加条件 select *from employee where phone like '%237%'
        //wrapper.条件的关键字（获取表中字段的名字，字段对应的值）
        //Employee::getPhone ====>phone
        wrapper.like(Employee::getPhone,"237");

        //3、调用查询方法
        List<Employee> list=employeeService.list(wrapper);
        return list;
    }
    // 性别等于1的按照id进行降序
    @GetMapping("/getBySex")
    public List<Employee> getBySex(){
        //1、实例化条件构造器
        LambdaQueryWrapper<Employee> wrapper  = new LambdaQueryWrapper<>();
        //2、添加条件 select *from employee where sex=1 order by id desc
        wrapper.eq(Employee::getSex,1)
                .orderByDesc(Employee::getId);
        //3、调用查询方法
        List<Employee> list=employeeService.list(wrapper);
        return list;
    }
    // > < = >= <= 对应的方法名
    // 根据用户名查询
}